package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单词书籍管理控制器
 */
@RestController
@RequestMapping("/admin/voc")
public class VocBookController {

    @Autowired
    private VocBookService vocBookService;

    /**
     * 获取书籍列表
     *
     * @param pageQuery 分页查询请求
     * @return 分页查询结果
     */
    @GetMapping("/book/list")
    public Result<PageDTO<VocBookDTO>> getVocBooks(VocBookPageRequest pageQuery) {
        PageDTO<VocBookDTO> pageDTO = vocBookService.getBookPage(pageQuery);
        return Result.ok(pageDTO);
    }

    /**
     * 添加单词书籍
     *
     * @param command 书籍请求体对象
     * @return 新增书籍的ID
     */
    @PostMapping("/book")
    public Result<Long> addVocBook(@RequestBody VocBookCommand command) {
        Long bookId = vocBookService.addVocBook(command);
        return Result.ok(bookId);
    }

    /**
     * 更新单词书籍
     *
     * @param command 书籍请求体对象
     * @return 空结果
     */
    @PutMapping("/book")
    public Result<Void> updateVocBook(@RequestBody VocBookCommand command) {
        vocBookService.updateVocBook(command);
        return Result.ok();
    }

    /**
     * 删除单词书籍
     *
     * @param bookId 书籍ID
     * @return 删除操作日志
     */
    @DeleteMapping("/book/{bookId}")
    public Result<DeleteBookOperateDTO> deleteVocBook(@PathVariable Long bookId) {
        DeleteBookOperateDTO operateDTO = vocBookService.deleteVocBook(bookId);
        return Result.ok(operateDTO);
    }

    /**
     * 批量删除单词书籍
     *
     * @param command 批量删除请求体对象
     * @return 删除操作日志列表
     */
    @DeleteMapping("/books")
    public Result<List<DeleteBookOperateDTO>> deleteVocBooks(@RequestBody BatchDeleteCommand command) {
        List<DeleteBookOperateDTO> operateDTOList = vocBookService.deleteVocBooks(command);
        return Result.ok(operateDTOList);
    }
}
